<?php
declare(strict_types=1);

namespace App\Controller\Admin\V1\Cms;

use App\Controller\Admin\AdminController;
use App\Helper\Utils;
use App\Model\CmsPost;
use App\Request\CmsPostRequest;
use App\Repositories\CmsPostRepository;

class PostsController extends AdminController
{
    public function list()
    {
        $category_id = $this->request->input('category_id',0);
        $title = $this->request->input('title',null);
        $rows = $this->request->input('rows',10);
        $search = [
            'category_id' => ['field'=>'category_id','value'=>$category_id]
        ];
        if($title){
            $search['title'] = [
                'field'=>'title',
                'value'=>$title,
                'method' => 'like'
            ];
        }
        $orderBy= [
            [
                'field' => 'id',
                'sort' => 'desc'
            ]
        ];
        $datas = CmsPostRepository::getInstance()->getList('*',$search,['limit'=>$rows],$orderBy);
        return $this->response->success($datas);

    }

    public function articles()
    {
        $title = $this->request->input('title',null);
        $rows = $this->request->input('rows',20);
        $search = [
            'type' => [
                'field' => 'type',
                'value' => CmsPost::TYPE_ARTICLE
            ]
        ];
        if($title){
            $search['title'] = [
                'field'=>'title',
                'value'=>$title,
                'method' => 'like'
            ];
        }
        $orderBy= [
            [
                'field' => 'id',
                'sort' => 'desc'
            ]
        ];
        $datas = CmsPostRepository::getInstance()->getList('*',$search,['limit'=>$rows],$orderBy);
        return $this->response->success($datas);

    }

    public function detail()
    {
        $id = $this->request->input('id');
        $post = CmsPostRepository::getInstance()->getById($id);
        return $this->response->success($post);
    }

    public function page()
    {
        $category_id = $this->request->input('category_id',0);
        $post = CmsPostRepository::getInstance()->one('*',[[
            'field' => 'type',
            'value' => CmsPost::TYPE_PAGE
        ],['field' => 'category_id','value'=>$category_id]]);
        $return = $post?$post:new \stdClass();
        return $this->response->success($return);
    }

    public function store(CmsPostRequest $request)
    {
        $result = CmsPostRepository::getInstance()->store($request->all());
        if($result['success']){
            return $this->response->success([
                'id' => $result['data']->id
            ]);
        }
        return $this->response->error(10000,$result['msg']);
    }

    public function delete()
    {
        $id = $this->request->input('id');
        $return = CmsPostRepository::getInstance()->delete($id);
        if($return){
            return $this->response->success([]);
        }
        return $this->response->error(10000,$return['msg']);
    }
}
